home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / DUMPCLAS.TAR / util / MethodInfo.java < prev   
Encoding:
Java Source  |  1996-05-22  |  4.2 KB  |  138 lines

  1. /*
  2.  * @(#)MethodInfo.java    1.4 95/08/16 Chuck McManis
  3.  *
  4.  * Copyright (c) 1996 Chuck McManis, All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies.
  10.  *
  11.  * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
  12.  * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  13.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  14.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS SHALL NOT BE
  15.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
  16.  * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  17.  */
  18.  
  19. package util;
  20.  
  21. import java.io.DataInputStream;
  22. import java.io.DataOutputStream;
  23. import java.io.IOException;
  24.  
  25. /**
  26.  * This class describes a Method as it is stored in the class file.
  27.  * The attribute associated with method is the code that actually implements
  28.  * the method. Since we don't need to manipulate the byte codes directly
  29.  * we leave them as an opaque chunk in the attributes[] array. References
  30.  * in the code are all references into the constant table so when we are
  31.  * modifing a class to use a different object we needn't get into the code
  32.  * level.
  33.  *
  34.  * @version     1.4, 16 Aug 1995
  35.  * @author    Chuck McManis
  36.  * @see        ClassFile
  37.  */
  38.  
  39. public class MethodInfo {
  40.     short         accessFlags;
  41.     ConstantPoolInfo     name;
  42.     ConstantPoolInfo     signature;
  43.     AttributeInfo    attributes[];
  44.  
  45.     /**
  46.      * Read a method_info from the data stream.
  47.      */
  48.     public boolean read(DataInputStream di, ConstantPoolInfo pool[])
  49.     throws IOException {
  50.     int    count;
  51.  
  52.     accessFlags = di.readShort();
  53.     name = pool[di.readShort()];
  54.     signature = pool[di.readShort()];
  55.     count = di.readShort();
  56.     if (count != 0) {
  57.         attributes = new AttributeInfo[count];
  58.         for (int i = 0; i < count; i++) {
  59.         attributes[i] = new AttributeInfo(); // "code"
  60.         if (! attributes[i].read(di, pool)) {
  61.             return (false);
  62.         }
  63.         }
  64.     }
  65.     return (true);
  66.     }
  67.  
  68.     /**
  69.      * Write out a method_info, do constant table fixups on the write.
  70.      */
  71.     public void write(DataOutputStream dos, ConstantPoolInfo pool[])
  72.     throws IOException, Exception {
  73.     dos.writeShort(accessFlags);
  74.     dos.writeShort(ConstantPoolInfo.indexOf(name, pool));
  75.     dos.writeShort(ConstantPoolInfo.indexOf(signature, pool));
  76.     if (attributes == null) {
  77.         dos.writeShort(0);
  78.     } else {
  79.         dos.writeShort(attributes.length);
  80.         for (int i = 0; i < attributes.length; i++)
  81.         attributes[i].write(dos, pool);
  82.     }
  83.     }
  84.  
  85.     /**
  86.      * print out the method, much as you would see it in the source
  87.      * file. The string ClassName is substituted for <init> when
  88.      * printing.
  89.      */
  90.     public String toString(String className) {
  91.     StringBuffer x = new StringBuffer();
  92.     boolean isArray = false;
  93.     String paramSig;
  94.     String returnSig;
  95.     int ndx = 0;
  96.     StringBuffer parameterList = new StringBuffer();
  97.     char    initialParameter = 'a';
  98.     StringBuffer varName = new StringBuffer();
  99.  
  100.  
  101.     String s = signature.strValue;
  102.     paramSig = s.substring(s.indexOf('(')+1, s.indexOf(')'));
  103.     returnSig = s.substring(s.indexOf(')')+1);
  104.  
  105.     x.append(ClassFile.accessString(accessFlags));
  106.     /* catch constructors */
  107.     if ((className != null) && (name.toString().startsWith("<init>")))
  108.         parameterList.append(className);
  109.     else
  110.         parameterList.append(name.toString());
  111.     parameterList.append("(");
  112.     if ((paramSig.length() > 0) && paramSig.charAt(0) != 'V') {
  113.         while (paramSig.length() > 0) {
  114.         varName.setLength(0);
  115.         varName.append(initialParameter);
  116.         initialParameter++;
  117.         parameterList.append(
  118.             ClassFile.typeString(paramSig, varName.toString()));
  119.         paramSig = ClassFile.nextSig(paramSig);
  120.         if (paramSig.length() > 0)
  121.             parameterList.append(", ");
  122.         }
  123.  
  124.         }
  125.     parameterList.append(")");
  126.     x.append(ClassFile.typeString(returnSig, parameterList.toString()));
  127.     x.append(";");
  128.     return (x.toString());
  129.     }
  130.  
  131.     /**
  132.      * Generic toString method, init method is unchanged.
  133.      */
  134.     public String toString() {
  135.     return (toString((String)null));
  136.     }
  137. }
  138.